home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Peter Lewis / PNL Libraries / MyUtils.p < prev    next >
Encoding:
Text File  |  1994-09-08  |  8.1 KB  |  377 lines  |  [TEXT/PJMM]

  1. unit MyUtils;
  2.  
  3. interface
  4.  
  5.     uses
  6. {$IFC undefined THINK_Pascal}
  7.         Events, 
  8.  {$ENDC}
  9.         MyTypes;
  10.  
  11.     function TrapAvailable (tNumber: INTEGER): BOOLEAN;
  12.     function MyNumToString (n: longInt): str255;
  13.     function NumToStr (n: longInt): str255;
  14.     function NN (n: longInt; len: integer): str31;
  15.     function N2 (n: longInt): str31;
  16.     function StrToNum (s: str255): longInt;
  17.     procedure DotDotDot (var s: str255; var width: integer);
  18.     function MyFrontWindow: boolean;
  19.     function DAFrontWindow: boolean;
  20.     function GetIndStrSize (size, id, index: integer): str255;
  21.     function GetDirID (wdrn: integer; var vrn: integer; var dirID: longInt): OSErr;
  22.     function GetVolInfo (var name: str63; var vrn: integer; index: integer; var CrDate: longInt): OSErr;
  23.     procedure PlotSICN (id, index, v, h: integer);
  24.     function LookupStrh (id: integer; match: str255): str255;
  25.     function LookupStrhNumber (id: integer; n: longInt): str255;
  26.     procedure BlockZero (p: ptr; len: longInt);
  27.     procedure BlockFill (p: univ ptr; len: longInt; value: integer);
  28.     function CheckCancel: boolean;
  29.     procedure TrashHandle (h: handle);
  30.     function WindowInWindowList (w: windowPtr): boolean;
  31.     function DirtyKey (ch: char): boolean;
  32.     function SendCharToIsDialogEvent (var er: EventRecord; cs: charSet): boolean;
  33.     procedure HiliteInvertRect (r: rect);
  34.  
  35.     procedure FixScrap;
  36.     procedure HaveResources;
  37.  
  38. implementation
  39.  
  40.     uses
  41. {$IFC undefined THINK_Pascal}
  42.         Desk, Scrap, Packages, Windows, ToolUtils, Resources, Memory, 
  43.   {$ENDC}
  44.         Folders, Traps, MyStrings;
  45.  
  46.     function TrapAvailable (tNumber: INTEGER): BOOLEAN;
  47. {Check to see if a given trap is implemented. Babble as taken from IM6 }
  48.         const
  49.             TrapMask = $0800;
  50.         var
  51.             tType: TrapType;
  52.     begin
  53.         if BAND(tNumber, TrapMask) > 0 then begin
  54.             tType := ToolTrap;
  55.         end
  56.         else begin
  57.             tType := OSTrap;
  58.         end;
  59.         if tType = ToolTrap then begin
  60.             tNumber := BAND(tNumber, $7FF);
  61.             if tNumber >= $400 then begin
  62.                 tNumber := _Unimplemented;
  63.             end
  64.             else if tNumber >= 512 then begin { 512 = $200, but that tickles a MW compiler bug }
  65.                 if NGetTrapAddress($A86E, ToolTrap) <> NGetTrapAddress($AA6E, ToolTrap) then begin
  66.                     tNumber := _Unimplemented;
  67.                 end;
  68.             end;
  69.         end;
  70.         TrapAvailable := NGetTrapAddress(tNumber, tType) <> GetTrapAddress(_Unimplemented);
  71.     end; {TrapAvailable}
  72.  
  73.     function MyNumToString (n: longInt): str255;
  74.         var
  75.             s: str255;
  76.     begin
  77.         if abs(n) < 4096 then
  78.             NumToString(n, s)
  79.         else if abs(n) < 4194304 then begin
  80.             NumToString(n div 1024, s);
  81.             s := Concat(s, 'k');
  82.         end
  83.         else begin
  84.             NumToString(n div 1048576, s);
  85.             s := Concat(s, 'M');
  86.         end;
  87.         MyNumToString := s;
  88.     end;
  89.  
  90.     function NumToStr (n: longInt): str255;
  91.         var
  92.             s: str255;
  93.     begin
  94.         NumToString(n, s);
  95.         NumToStr := s;
  96.     end;
  97.  
  98.     function NN (n: longInt; len: integer): str31;
  99.         var
  100.             s: str31;
  101.     begin
  102.         s := NumToStr(n);
  103.         while length(s) < len do
  104.             s := concat('0', s);
  105.         NN := s;
  106.     end;
  107.  
  108.     function N2 (n: longInt): str31;
  109.     begin
  110.         N2 := NN(n, 2);
  111.     end;
  112.  
  113.     function StrToNum (s: str255): longInt;
  114.         var
  115.             n: longInt;
  116.     begin
  117.         StringToNum(s, n);
  118.         StrToNum := n;
  119.     end;
  120.  
  121.     procedure DotDotDot (var s: str255; var width: integer);
  122.         var
  123.             maxwidth, len: integer;
  124.     begin
  125.         maxwidth := width;
  126.         width := StringWidth(s);
  127.         if width > maxwidth then begin
  128.             width := width + CharWidth('…');
  129. {$PUSH}
  130. {$R-}
  131.             len := ord(s[0]);
  132.             while (len > 0) and (width > maxwidth) do begin
  133.                 width := width - CharWidth(s[len]);
  134.                 len := len - 1;
  135.             end;
  136.             len := len + 1;
  137.             s[0] := chr(len);
  138.             s[len] := '…';
  139. {$POP}
  140.         end;
  141.     end;
  142.  
  143.     function MyFrontWindow: boolean;
  144.         var
  145.             wp: windowPtr;
  146.     begin
  147.         wp := FrontWindow;
  148.         if wp = nil then
  149.             MyFrontWindow := false
  150.         else
  151.             MyFrontWindow := windowPeek(wp)^.windowKind >= userKind;
  152.     end;
  153.  
  154.     function DAFrontWindow: boolean;
  155.         var
  156.             wp: windowPtr;
  157.     begin
  158.         wp := FrontWindow;
  159.         if wp = nil then
  160.             DAFrontWindow := false
  161.         else
  162.             DAFrontWindow := windowPeek(wp)^.windowKind < 0;
  163.     end;
  164.  
  165.     function GetIndStrSize (size, id, index: integer): str255;
  166.         var
  167.             s: str255;
  168.     begin
  169.         GetIndString(s, id, index);
  170.         GetIndStrSize := TPcopy(s, 1, size - 1);
  171.     end;
  172.  
  173.     function GetDirID (wdrn: integer; var vrn: integer; var dirID: longInt): OSErr;
  174.         var
  175.             procID: longInt;
  176.             oe: OSErr;
  177.     begin
  178.         oe := GetWDInfo(wdrn, vrn, dirID, procID);
  179.         if oe <> noErr then begin
  180.             vrn := wdrn;
  181.             dirID := 0;
  182.         end;
  183.         GetDirID := oe;
  184.     end;
  185.  
  186.     function GetVolInfo (var name: str63; var vrn: integer; index: integer; var CrDate: longInt): OSErr;
  187.         var
  188.             pb: paramBlockRec;
  189.             oe: OSErr;
  190.     begin
  191.         if (name <> '') & (name[length(name)] <> ':') then
  192.             name := concat(name, ':');
  193.         pb.ioNamePtr := @name;
  194.         pb.ioVRefNum := vrn;
  195.         pb.ioVolIndex := index;
  196.         oe := PBGetVInfo(@pb, false);
  197.         if oe = noErr then begin
  198.             vrn := pb.ioVRefNum;
  199.             CrDate := pb.ioVCrDate;
  200.         end;
  201.         GetVolInfo := oe;
  202.     end;
  203.  
  204.     procedure PlotSICN (id, index, v, h: integer);
  205.         var
  206.             sh: Handle;
  207.             bm: BitMap;
  208.             r: Rect;
  209.             gp: grafptr;
  210.     begin
  211.         sh := GetResource('SICN', id);
  212.         HLock(sh);
  213.         bm.baseAddr := Ptr(longInt(sh^) + (index - 1) * 32);
  214.         bm.rowBytes := 2;
  215.         SetRect(r, h, v, h + 16, v + 16);
  216.         bm.bounds := r;
  217.         GetPort(gp);
  218.         CopyBits(bm, gp^.portBits, r, r, srcCopy, nil);
  219.         HUnlock(sh);
  220.         HPurge(sh);
  221.     end;
  222.  
  223.     function LookupStrh (id: integer; match: str255): str255;
  224.         var
  225.             t, s: str255;
  226.             i: integer;
  227.     begin
  228.         t := '';
  229.         i := 1;
  230.         repeat
  231.             GetIndString(s, id, i);
  232.             if s = match then begin
  233.                 GetIndString(t, id, i + 1);
  234.                 leave;
  235.             end;
  236.             i := i + 2;
  237.         until s = '';
  238.         LookupStrh := t;
  239.     end;
  240.  
  241.     function LookupStrhNumber (id: integer; n: longInt): str255;
  242.         var
  243.             s, t: str255;
  244.     begin
  245.         NumToString(n, s);
  246.         t := LookupStrh(id, s);
  247.         if t = '' then
  248.             t := s;
  249.         LookupStrhNumber := t;
  250.     end;
  251.  
  252.     procedure TrashHandle (h: handle);
  253.         var
  254.             p: ptr;
  255.             i: longInt;
  256.     begin
  257.         if (h <> nil) & (h^ <> nil) then begin
  258.             p := h^;
  259.             for i := 1 to GetHandleSize(h) do begin
  260.                 p^ := -27;
  261.                 longInt(p) := longInt(p) + 1;
  262.             end;
  263.         end;
  264.     end;
  265.  
  266.     function CheckCancel: boolean;
  267.         var
  268.             er: eventRecord;
  269.     begin
  270.         if GetNextEvent(everyEvent, er) then begin
  271.             CheckCancel := (er.what = keyDown) and (BAND(er.message, charCodeMask) = ord('.')) and (BAND(er.modifiers, cmdKey) <> 0)
  272.         end
  273.         else begin
  274.             CheckCancel := false;
  275.         end;
  276.     end;
  277.  
  278.     procedure BlockZero (p: ptr; len: longInt);
  279.         var
  280.             i: longInt;
  281.     begin
  282.         if len > 0 then begin
  283.             while (BAND(ord(p), 3) <> 0) & (len > 0) do begin
  284.                 p^ := 0;
  285.                 longInt(p) := longInt(p) + 1;
  286.                 len := len - 1;
  287.             end;
  288.             while len >= 4 do begin
  289.                 longIntPtr(p)^ := 0;
  290.                 longInt(p) := longInt(p) + 4;
  291.                 len := len - 4;
  292.             end;
  293.             while len > 0 do begin
  294.                 p^ := 0;
  295.                 longInt(p) := longInt(p) + 1;
  296.                 len := len - 1;
  297.             end;
  298.         end
  299.     end;
  300.  
  301.     procedure BlockFill (p: univ ptr; len: longInt; value: integer);
  302.     begin
  303.         while (len > 0) do begin
  304.             p^ := value;
  305.             len := len - 1;
  306.             longInt(p) := longInt(p) + 1;
  307.         end;
  308.     end;
  309.  
  310.     function WindowInWindowList (w: windowPtr): boolean;
  311.         type
  312.             windowPtrPtr = ^windowPtr;
  313.         var
  314.             nw: windowPtr;
  315.     begin
  316.         nw := windowPtrPtr($9D6)^;
  317.         while (nw <> nil) & (w <> nw) do begin
  318.             nw := windowPtr(windowPeek(nw)^.nextwindow);
  319.         end;
  320.         WindowInWindowList := nw <> nil;
  321.     end;
  322.  
  323.     function DirtyKey (ch: char): boolean;
  324.     begin
  325.         case ord(ch) of
  326.             homeChar, endChar, helpChar, pageUpChar, pageDownChar, leftArrowChar, rightArrowChar, upArrowChar, downArrowChar: 
  327.                 DirtyKey := false;
  328.             otherwise
  329.                 DirtyKey := true;
  330.         end;
  331.     end;
  332.  
  333.     function SendCharToIsDialogEvent (var er: EventRecord; cs: charSet): boolean;
  334.         var
  335.             ch: char;
  336.     begin
  337.         SendCharToIsDialogEvent := true;
  338.         if ((er.what = keyDown) | (er.what = autoKey)) & (BAND(er.modifiers, cmdKey) = 0) then begin
  339.             ch := chr(BAND(er.message, $FF));
  340.             if not (ch in (cs + [tab, del, bs])) & DirtyKey(ch) then begin
  341.                 SendCharToIsDialogEvent := false;
  342.             end;
  343.         end;
  344.     end;
  345.  
  346.     procedure HiliteInvertRect (r: rect);
  347.         const
  348.             HiliteMode = $938;
  349.     begin
  350.         BitClr(POINTER(HiliteMode), pHiliteBit);
  351.         InvertRect(r);
  352.     end;
  353.  
  354. {$S Main}
  355.     procedure FixScrap;
  356.         var
  357.             scrap: PScrapStuff;
  358.             junk, offset: longInt;
  359.     begin
  360.         scrap := InfoScrap;
  361.         if scrap^.scrapHandle = nil then begin
  362.             scrap^.scrapState := -1;
  363.         end;
  364.         junk := GetScrap(nil, 'XXXX', offset);
  365.         junk := UnloadScrap;
  366.     end;
  367.  
  368. {$S Main}
  369.     procedure HaveResources;
  370.     begin
  371.         if Get1Resource('BNDL', 128) = nil then begin
  372.             SysBeep(1);
  373.             ExitToShell;
  374.         end;
  375.     end;
  376.  
  377. end.